package com.aiyige.page.globalsearch.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.page.detail.DetailPage;
import com.aiyige.page.globalsearch.model.SearchGroup;
import com.aiyige.page.globalsearch.util.SearchUtil;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.ListUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/12/11.
 */

public class GroupSearchAdapter extends BaseQuickAdapter<SearchGroup, BaseViewHolder> {

    public GroupSearchAdapter() {
        super(new LinkedList<SearchGroup>());
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchGroup item) {
        switch (item.getTypeId()) {
            case SearchUtil.TYPE_CLASSROOM_RENTAL:
                ((ClassroomViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_DYNAMIC:
                ((DynamicViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_LEARN_VIDEO:
                ((LearnVideoViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_MAJOR_COURSE:
                ((MajorCourseViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_PERSONAL_TRAINER:
                ((PersonalTrainerViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_TRAING_CARD:
                ((TraingCardViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_TRAING_CLASS:
                ((TraingClassViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_UNIVERSAL_PRODUCT:
                ((UniversalProductViewHolder) helper).bindData(item);
                break;
        }
    }

    @Override
    protected int getDefItemViewType(int position) {
        return mData.get(position).getTypeId();
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewholder;
        switch (viewType) {
            case SearchUtil.TYPE_CLASSROOM_RENTAL:
                baseViewholder = new ClassroomViewHolder(getItemView(R.layout.global_group_search_result_item, parent));
                break;
            case SearchUtil.TYPE_DYNAMIC:
                baseViewholder = new DynamicViewHolder(getItemView(R.layout.global_group_search_result_item, parent));
                break;
            case SearchUtil.TYPE_LEARN_VIDEO:
                baseViewholder = new LearnVideoViewHolder(getItemView(R.layout.global_group_search_result_item, parent));
                break;
            case SearchUtil.TYPE_MAJOR_COURSE:
                baseViewholder = new MajorCourseViewHolder(getItemView(R.layout.global_group_search_result_item, parent));
                break;
            case SearchUtil.TYPE_PERSONAL_TRAINER:
                baseViewholder = new PersonalTrainerViewHolder(getItemView(R.layout.global_group_search_result_item, parent));
                break;
            case SearchUtil.TYPE_TRAING_CARD:
                baseViewholder = new TraingCardViewHolder(getItemView(R.layout.global_group_search_result_item, parent));
                break;
            case SearchUtil.TYPE_UNIVERSAL_PRODUCT:
                baseViewholder = new UniversalProductViewHolder(getItemView(R.layout.global_group_search_result_item, parent));
                break;
            case SearchUtil.TYPE_TRAING_CLASS:
                baseViewholder = new TraingClassViewHolder(getItemView(R.layout.global_group_search_result_item, parent));
                break;
            default:
                baseViewholder = new BaseViewHolder(getItemView(R.layout.wild_card_item, parent));
                break;
        }

        return baseViewholder;
    }

    public class UniversalProductViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        public UniversalProductViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.moreLayout);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
        }

        public void bindData(SearchGroup data) {
            searchResultTitleTv.setText(data.getName() + "(" + data.getTotal() + ")");
            if (ListUtil.isEmpty(data.getSamples())) {
                noResultTv.setVisibility(View.VISIBLE);
                moreLayout.setVisibility(View.INVISIBLE);
            } else {
                moreLayout.setVisibility(View.VISIBLE);
                noResultTv.setVisibility(View.INVISIBLE);
            }
        }
    }

    public class TraingClassViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        TraingClassAdapter traingClassAdapter;

        public TraingClassViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.moreLayout);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            traingClassAdapter = new TraingClassAdapter();
            traingClassAdapter.bindToRecyclerView(recyclerView);
            traingClassAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Router.start(traingClassAdapter.getItem(position).getRouter());
                }
            });
        }

        public void bindData(SearchGroup data) {
            searchResultTitleTv.setText(data.getName() + "(" + data.getTotal() + ")");
            if (ListUtil.isEmpty(data.getSamples())) {
                noResultTv.setVisibility(View.VISIBLE);
                moreLayout.setVisibility(View.INVISIBLE);
                traingClassAdapter.setNewData(new LinkedList<SearchGroup.Sample>());
            } else {
                if (data.getTotal() <= SearchGroup.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
                traingClassAdapter.setNewData(data.getSamples().subList(0, Math.min(data.getSamples().size(), SearchGroup.MAX_SAMPLE_NUM)));
            }
        }
    }

    public class TraingCardViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        TraingCardAdapter traingCardAdapter;

        public TraingCardViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.moreLayout);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            traingCardAdapter = new TraingCardAdapter();
            traingCardAdapter.bindToRecyclerView(recyclerView);
            traingCardAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Router.start(traingCardAdapter.getItem(position).getRouter());
                }
            });
        }

        public void bindData(SearchGroup data) {
            searchResultTitleTv.setText(data.getName() + "(" + data.getTotal() + ")");
            if (ListUtil.isEmpty(data.getSamples())) {
                noResultTv.setVisibility(View.VISIBLE);
                moreLayout.setVisibility(View.INVISIBLE);
                traingCardAdapter.setNewData(new LinkedList<SearchGroup.Sample>());
            } else {
                if (data.getTotal() <= SearchGroup.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
                traingCardAdapter.setNewData(data.getSamples().subList(0, Math.min(data.getSamples().size(), SearchGroup.MAX_SAMPLE_NUM)));
            }
        }
    }


    public class PersonalTrainerViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        public PersonalTrainerViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.moreLayout);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
        }

        public void bindData(SearchGroup data) {
            searchResultTitleTv.setText(data.getName() + "(" + data.getTotal() + ")");
            if (ListUtil.isEmpty(data.getSamples())) {
                noResultTv.setVisibility(View.VISIBLE);
                moreLayout.setVisibility(View.INVISIBLE);
            } else {
                if (data.getTotal() <= SearchGroup.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
        }
    }

    public class MajorCourseViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        MajorCourseAdapter majorCourseAdapter;

        public MajorCourseViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.moreLayout);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            majorCourseAdapter = new MajorCourseAdapter();
            majorCourseAdapter.bindToRecyclerView(recyclerView);
            majorCourseAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Router.start(majorCourseAdapter.getItem(position).getRouter());
                }
            });
        }

        public void bindData(SearchGroup data) {
            if (data.getTotal() != 0) {
                searchResultTitleTv.setText(data.getName() + "(" + data.getTotal() + ")");
            } else {
                searchResultTitleTv.setText(data.getName());
            }
            if (ListUtil.isEmpty(data.getSamples())) {
                noResultTv.setVisibility(View.VISIBLE);
                moreLayout.setVisibility(View.INVISIBLE);
                majorCourseAdapter.setNewData(new LinkedList<SearchGroup.Sample>());
            } else {
                if (data.getTotal() <= SearchGroup.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
                majorCourseAdapter.setNewData(data.getSamples().subList(0, Math.min(data.getSamples().size(), SearchGroup.MAX_SAMPLE_NUM)));
            }
        }
    }

    public class LearnVideoViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        LearnVideoAdapter learnVideoAdapter;

        public LearnVideoViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.moreLayout);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            learnVideoAdapter = new LearnVideoAdapter();
            learnVideoAdapter.bindToRecyclerView(recyclerView);
            learnVideoAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Router.start(learnVideoAdapter.getItem(position).getRouter());
                }
            });
        }

        public void bindData(SearchGroup data) {
            if (data.getTotal() != 0) {
                searchResultTitleTv.setText(data.getName() + "(" + data.getTotal() + ")");
            } else {
                searchResultTitleTv.setText(data.getName());
            }
            if (ListUtil.isEmpty(data.getSamples())) {
                noResultTv.setVisibility(View.VISIBLE);
                moreLayout.setVisibility(View.INVISIBLE);
                learnVideoAdapter.setNewData(new LinkedList<SearchGroup.Sample>());
            } else {
                if (data.getTotal() <= SearchGroup.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            learnVideoAdapter.setNewData(data.getSamples().subList(0, Math.min(data.getSamples().size(), SearchGroup.MAX_SAMPLE_NUM)));
        }
    }

    public class DynamicViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        DynamicAdapter dynamicAdapter;

        public DynamicViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.moreLayout);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            dynamicAdapter = new DynamicAdapter();
            dynamicAdapter.bindToRecyclerView(recyclerView);
            dynamicAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    SearchGroup.Sample sample = dynamicAdapter.getItem(position);
                    switch (sample.getType()) {
                        case SearchUtil.TYPE_INFO:
                            Router.start(sample.getRouter());
                            break;
                        case SearchUtil.TYPE_VIDEO:
                            Router.start(sample.getRouter() + "");
                            break;
                        case SearchUtil.TYPE_PHOTO:
                            Router.start(sample.getRouter() + "");
                            break;
                    }
                }
            });
        }

        public void bindData(SearchGroup data) {
            if (data.getTotal() != 0) {
                searchResultTitleTv.setText(data.getName() + "(" + data.getTotal() + ")");
            } else {
                searchResultTitleTv.setText(data.getName());
            }
            if (ListUtil.isEmpty(data.getSamples())) {
                noResultTv.setVisibility(View.VISIBLE);
                moreLayout.setVisibility(View.INVISIBLE);
                dynamicAdapter.setNewData(new LinkedList<SearchGroup.Sample>());
            } else {
                if (data.getTotal() <= SearchGroup.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            dynamicAdapter.setNewData(data.getSamples().subList(0, Math.min(data.getSamples().size(), SearchGroup.MAX_SAMPLE_NUM)));
        }
    }


    public class ClassroomViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        public ClassroomViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.moreLayout);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
        }

        public void bindData(SearchGroup data) {
            searchResultTitleTv.setText(data.getName() + "(" + data.getTotal() + ")");
            if (ListUtil.isEmpty(data.getSamples())) {
                noResultTv.setVisibility(View.VISIBLE);
                moreLayout.setVisibility(View.INVISIBLE);
            } else {
                if (data.getTotal() <= SearchGroup.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
        }
    }
}
